package ua.kpi_java_training.evgeniygusar.module3;

public class Leopard extends InhabitantOfTheDesert {

    int NUMBERS_STEPS_OF_LEOPARD = 5;
    int LEOPARD_BIRTH_CYCLE = 3;
    public int x_of_nearest_ostrich, y_of_nearest_ostrich;
    int index_of_nearest_ostrich = 0;

    AllOstriches ostrichesObject = new AllOstriches();
    AllLeopards leopardsObject = new AllLeopards();

    /**
     * Getter for LEOPARD_BIRTH_CYCLE of leopards
     */
    int getLeopardBirthCycle() {
        return LEOPARD_BIRTH_CYCLE;
    }

    /**
     * Algorith of leopard's moving
     */

    public void Moving() {
        int current_step=1;
        ostrichesObject.nearestOstrich(x, y);
        x_of_nearest_ostrich = ostrichesObject.getXOfNearestOstrich();
        y_of_nearest_ostrich = ostrichesObject.getYOfNearestOstrich();
        index_of_nearest_ostrich = ostrichesObject.getIndexOfNearestOstrich();
        checkOstrichIsEated();
        while ((current_step <= NUMBERS_STEPS_OF_LEOPARD)&(ostrichesObject.numbersOfOstriches() != 0)) {
            if (y != y_of_nearest_ostrich) {
                stepY();
                displayCurrentStep(current_step);
                checkOstrichIsEated();
            }
            else if ((y == y_of_nearest_ostrich)&(x != x_of_nearest_ostrich)) {
                stepX();
                displayCurrentStep(current_step);
                checkOstrichIsEated();
            }
            current_step++;
        }
    }

    Destination des;

    /**
     * Step y of leopard. Moving on diagonal
     */

    public void stepY() {
        if (y > y_of_nearest_ostrich) {
            if (x > x_of_nearest_ostrich)
                Move(des.up_left);
            else if(x < x_of_nearest_ostrich)
                Move(des.up_right);
            else if((x == x_of_nearest_ostrich)&(y%2==0))
                Move(des.up_left);
            else if((x == x_of_nearest_ostrich)&(y%2!=0))
                Move(des.up_right);
        }
        else if (y < y_of_nearest_ostrich) {
            if (x > x_of_nearest_ostrich)
                Move(des.down_left);
            else if(x < x_of_nearest_ostrich)
                Move(des.down_right);
            else if((x == x_of_nearest_ostrich)&(y%2==0))
                Move(des.down_left);
            else if((x == x_of_nearest_ostrich)&(y%2!=0))
                Move(des.down_right);
        }
    }

    /**
     * Moving of leopard on x line when leopard and ostrich have same y coordinars
     */

    public void stepX() {
        if (x > x_of_nearest_ostrich)
            x--;
        else if (x < x_of_nearest_ostrich)
            x++;
    }

    /**
     * Output of current step number
     * @param current_step current step of leopard
     */

    void displayCurrentStep(int current_step) {
        System.out.print("   Step " + current_step + ": ");
        showCoordinats();
    }

    public void checkOstrichIsEated() {
        if((x == x_of_nearest_ostrich) & (y == y_of_nearest_ostrich)) {
            displayOstrichIsEated();

            ostrichesObject.removeOstrich(index_of_nearest_ostrich);
            leopardsObject.incrementTotalNumberOfEatedOstriches();
            if (ostrichesObject.numbersOfOstriches() != 0) {
                ostrichesObject.nearestOstrich(x, y);
                x_of_nearest_ostrich = ostrichesObject.getXOfNearestOstrich();
                y_of_nearest_ostrich = ostrichesObject.getYOfNearestOstrich();
                index_of_nearest_ostrich = ostrichesObject.getIndexOfNearestOstrich();
            }
        }
    }

    /**
     * function that display that ostrich is eated when it have the same coorinats as leopard
     */

    void displayOstrichIsEated() {
        System.out.println("ARGHHH!");
        System.out.println("Ostrich " + (index_of_nearest_ostrich+1) +
                " with coordinats: x = " + x_of_nearest_ostrich +
                ", y = " + y_of_nearest_ostrich + " is eated!");
    }
}

